package com.fumin.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fumin.pojo.BusinessResponse;
import com.fumin.pojo.Projects;
import com.fumin.service.ProjectsService;
import jnr.ffi.annotations.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * Created by Lingo on 2018/3/4.
 */
@Controller
public class ProjectController {

    @Resource
    ProjectsService projectsService;

//    @RequestMapping(value = "/project", method = RequestMethod.POST)
//    @ResponseBody
//    public Object cretaeProject(@RequestBody @JsonFormat Projects projects) {
//        BusinessResponse businessResponse = new BusinessResponse();
//        projects.setProject_status(0);
//        projectsService.insertSelective(projects);
//        Long pk = projects.getPk();
//        businessResponse.setCode("0");
//        businessResponse.setContent(pk);
//        businessResponse.setMessage("success");
//        return businessResponse;
//    }
    @RequestMapping(value = "/project/{pk}", method = RequestMethod.DELETE)
    @ResponseBody
    public Object removeProject(@PathVariable Long pk) {
        BusinessResponse businessResponse = new BusinessResponse();
        projectsService.deleteByPrimaryKey(pk);
        businessResponse.setCode("0");
        businessResponse.setMessage("success");
        return businessResponse;
    }
//    @RequestMapping(value = "/project/{pk}", method = RequestMethod.PUT)
//    @ResponseBody
//    public Object updateProject(@PathVariable Long pk,@RequestBody @JsonFormat Projects projects) {
//        BusinessResponse businessResponse = new BusinessResponse();
//        projects.setPk(pk);
//        projectsService.updateByPrimaryKeySelective(projects);
//        businessResponse.setCode("0");
//        businessResponse.setMessage("success");
//        return businessResponse;
//    }
    @RequestMapping(value = "/project/{pk}", method = RequestMethod.GET)
    @ResponseBody
    public Object queryProjectInfo(@PathVariable Long pk) {
        BusinessResponse businessResponse = new BusinessResponse();
        Projects projects = projectsService.selectByPrimaryKey(pk);
        businessResponse.setCode("0");
        businessResponse.setMessage("success");
        businessResponse.setContent(projects);
        return businessResponse;
    }
    @RequestMapping(value = "/project/list", method = RequestMethod.GET)
    @ResponseBody
    public Object queryProjectList(Integer page , Integer num) {
        BusinessResponse businessResponse = new BusinessResponse();
        Integer start = num*(page-1);
        Integer offset = num;
        List<Projects> projectsList = projectsService.selectPage(start,offset);
        businessResponse.setCode("0");
        businessResponse.setMessage("success");
        businessResponse.setContent(projectsList);
        return businessResponse;
    }

    @RequestMapping(value = "/project/list/status", method = RequestMethod.GET)
    @ResponseBody
    public Object queryProjectListByStatus(Integer status ,Integer page , Integer num) {
        BusinessResponse businessResponse = new BusinessResponse();
        Integer start = num*(page-1);
        Integer offset = num;
        List<Projects> projectsList = projectsService.selectPageByStatus(start,offset,status);
        businessResponse.setCode("0");
        businessResponse.setMessage("success");
        businessResponse.setContent(projectsList);
        return businessResponse;
    }


    @RequestMapping(value = "/project/count/status", method = RequestMethod.GET)
    @ResponseBody
    public Object queryProjectListByStatus(Integer status) {
        BusinessResponse businessResponse = new BusinessResponse();
        Map<String,Long> result = projectsService.selectNumByStatus(status);
        businessResponse.setCode("0");
        businessResponse.setMessage("success");
        businessResponse.setContent(result.get("num"));
        return businessResponse;
    }



}
